//样式
var enerPercentCellCss,addCellAttr;
//评价
var showComment;
require(['grid', 'topBar', 'date'], function () {
    //提交到后台参数
    var postData;
    $('#top').topBar({
        left: [
            {
                type: 'filter',
                search: function () {
                    search();
                },
                item: [
                    {
                        text: '时间快捷',
                        name: 'reportType',
                        targetId: 'beginTime',
                        type: 'reportDateSelect'
                    }, {
                        text: '查询时间',
                        id: 'beginTime',
                        name: 'beginTime',
                        type: 'text',
                        width: 250
                    }, {
                        text: '车型',
                        name: 'carType',
                        type: 'item-select',
                        dict: 'CAT00000'
                    }
                ]
            }
        ],
        right: [{
            text: '导出',
            type: 'export-btn',
            authority: '',
            item: [{
                text: 'Excel导出',
                click: function () {
                    exportFile('Excel');
                }
            }, {
                text: 'PDF导出',
                click: function () {
                    exportFile('Pdf');
                }
            }]
        }]
    });

    function exportFile(suffix) {
        if ($('#grid').getGridParam("reccount") < 1) {
            $.messager.alert("提示", "请先查询数据。", "info");
            return;
        }
        if (getPostData()) {
            var url = "/energySaving/exportEnergySaving" + suffix + ".htm";
            $.exportFile(url, postData);
        }
    }

    $('#grid').parent().height($(window).height() - $('#top').outerHeight() - 20);

    addCellAttr = function (rowId, val, rawObject, cm, rdata) {
        if ((rawObject.enerConsPetg > 50)||(rawObject.totalMile <= 0 && rawObject.totalEnerCons <= 0 && rawObject.totalDura > 0)) {
            return "style='color:red'";
        }
    };

    enerPercentCellCss = function (rowId, val, rawObject, cm, rdata){
        if (rawObject.enerPercent > 0) {
            return "style='font-weight:bold;color:red'";
        } else if (rawObject.enerPercent < 0) {
            return "style='font-weight:bold;color:green'";
        } else {
            return "style='font-weight:bold;'";
        }
    }

    $('#grid').grid({
        multiselect: false,
        url: "/energySaving/searchEnergySaving.htm",
        colModel: [{name: "lineId", label: "lineId", hidden: true},
            {name: "totalDura", label: "totalDura", hidden: true},
            {name: "enerConsPetg", label: "enerConsPetg", hidden: true},
            {name: "allRank", label: "排名", align: "right", cellattr: addCellAttr, width: 40},
            {name: "score", label: "得分", align: "right", cellattr: addCellAttr, width: 40},
            {name: "drvName", label: "驾驶员", cellattr: addCellAttr, width: 80},
            {name: "lineName", label: "营运线路", cellattr: addCellAttr, width: 80},
            {name: "organName", label: "所属机构", cellattr: addCellAttr, width: 80},
            {name: "lastingTime", label: "运行时长", align: "right", cellattr: addCellAttr, width: 80},
            {
                name: "totalMile", label: "运行里程(KM)", align: "right", width: 120,
                formatter: 'customCurrency', cellattr: addCellAttr
            },
            {
                name: "totalEnerCons", label: "运行能耗(L|m³|KWh)", align: "right", width: 160,
                formatter: 'customCurrency', cellattr: addCellAttr
            },
            {
                name: "hundKmEnerCons",
                label: "百公里能耗(L|m³|KWh/100KM)",
                align: "right",
                formatter: 'customCurrency',
                cellattr: addCellAttr, width: 260
            },
            {
                name: "trgtEnerCons",
                label: "目标能耗(L|m³|KWh)",
                align: "right",
                formatter: 'customCurrency',
                cellattr: addCellAttr, width: 160
            },
            {
                name: "enerPercent",
                label: "能耗百分比(%)",
                align: "right", width: 160,
                formatter: 'customCurrency',
                cellattr: enerPercentCellCss
            },
            {
                name: "lineAvrgEnerCons",
                label: "线路平均能耗(L|m³|KWh)",
                align: "right",
                formatter: 'customCurrency',
                cellattr: addCellAttr, width: 200
            },
            {name: "wthrEnersav", label: "是否节能", cellattr: addCellAttr, width: 80}
        ],
        onSelectRow: function (ids) {
            var data = $('#grid').getRowData(ids);
            showComment(data);
        }
    });

    showComment = function (obj) {
        if (obj.allRank == '-') {
            $("#showMsg").css("display", "none");
        } else if (obj.enerConsPetg == 0 && obj.totalMile == 0) {
            $("#showMsg").css("display", "none");
        } else {
            var param = $('#top').parent().getValues();
            param.compareValue = obj.enerConsPetg;
            param.organName = obj.organName;
            param.lineName = obj.lineName;
            param.drvName = obj.drvName;
            param.lineId = obj.lineId;
            $.ajaxExt({
                data: param,
                url: "/energySaving/getEnerComments.htm"
            }).done(function (result) {
                if (result[1] == "节能明星" || result[1] == "节能标兵") {
                    $("#title").html("<font style = 'color:green;'>" + result[1] + "</font>");
                } else if (result[1] == "节能能手" || result[1] == "一般") {
                    $("#title").html("<font style = 'color:orange;'>" + result[1] + "</font>");
                } else {
                    $("#title").html("<font style = 'color:red;'>" + result[1] + "</font>");
                }
                $("#comments").html("<b>评语:</b>" + result[0]);
                $("#score").html(obj.score);
                $("#enerCons").html(obj.enerPercent);
                $("#drvName").html(obj.drvName);
            });
            $("#showMsg").css("display", "block");
        }
    }
    function search() {
        if (getPostData()) {
            $('#grid').grid('setGridParam', {
                page: 1,
                datatype: "json",
                postData: postData
            }).trigger("reloadGrid");
        }
    }

    function getPostData() {
        postData = $('#top').parent().getValues();
        if(!postData.beginTime){
            $.messager.alert("提示", "请先选择查询时间。", "info");
            return false;
        }
        var nodes = commonFun.getLeftTreeCheckedNodes();
        if (nodes.LINE) {
            postData.orgIds = nodes.LINE;
            postData.type = "LINE";
            return true;
        }
        $.messager.alert("提示", "至少请选择一条线路。", "info");
        return false;
    }

})
